﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EMS系统定时任务
{
    public partial class frmMain : Form
    {
        public delegate void OAAddToListBox(string text);  // 委托


        public frmMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 在程序一开始的时候，将所有结束定时任务的按钮禁用
            this.btn_end_oa.Enabled = false;
            this.btn_equip_end.Enabled = false;

            // 初始化OA基础设置
            OABaseSetting();
            EquipBaseSetting();

            // 给OAJob定时任务委托赋值
            OAJob oAJob = new OAJob();
            OAJob.addMsgToListBOx = new AddMsgToListBox(ShowLog);

            EquipJob equipJob = new EquipJob();
            EquipJob.addMsgToListBOx= new AddMsgToListBox(ShowLogEquip);

        }

        #region OA资产定时任务
        /// <summary>
        /// OA定时任务启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_start_oa_Click(object sender, EventArgs e)
        {
            // 禁用启动按钮，开启结束按钮
            this.btn_end_oa.Enabled = true;
            this.btn_start_oa.Enabled = false;

            try
            {
                // 获取设置的时间
                //string CronTime = "0/1 * * * * ?";
                string CronTime = txtOACron.Text.Trim();
                OAListBoxSetting();
                listBoxOAItem.Items.Add("【" + DateTime.Now.ToString() + "】" + " --*-- OA资产数据定时任务开启  --*-- ");
                QuartzUtil.AddJob<OAJob>("OAJob", CronTime);

            }
            catch (Exception ex)
            {
                listBoxOAItem.Items.Add(ex.Message);
            }



        }
        /// <summary>
        /// OA定时任务结束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_end_oa_Click(object sender, EventArgs e)
        {
            // 禁用结束按钮，开启启动按钮
            this.btn_end_oa.Enabled = false;
            this.btn_start_oa.Enabled = true;

            try
            {
                // 结束定时任务
                QuartzUtil.DeleteJob("OAJob");
                OAListBoxSetting();
                listBoxOAItem.Items.Add("【" + DateTime.Now.ToString() + "】" + " --*-- OA资产数据定时任务结束  --*-- ");
            }
            catch (Exception ex)
            {
                listBoxOAItem.Items.Add(ex.Message);
            }
        }

        /// <summary>
        /// OA基础设置初始化
        /// </summary>
        public void OABaseSetting()
        {
            txtOACron.Enabled = false;
            txtListBoxNum.Enabled = false;
            btn_oa_setting_center.Enabled = false;
            btn_oa_setting_edit.Enabled = true;
        }

        /// <summary>
        /// OA基础设置编辑按钮点击要初始的
        /// </summary>
        public void OABaseSettingEdit()
        {
            txtOACron.Enabled = true;
            txtListBoxNum.Enabled = true;
            btn_oa_setting_center.Enabled = true;
            btn_oa_setting_edit.Enabled = false;
        }



        /// <summary>
        /// OA基础设置编辑按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_oa_setting_edit_Click(object sender, EventArgs e)
        {
            OABaseSettingEdit();
        }
        /// <summary>
        /// OA基础设置确定按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_oa_setting_center_Click(object sender, EventArgs e)
        {
            // 验证用户设置的是否合理
            bool isOk = OACenterVaild();

            if (isOk)
            {
                // 初始化设置
                OABaseSetting();
            }
        }

        /// <summary>
        /// OA基础设置点击确认验证方法
        /// </summary>
        public bool OACenterVaild()
        {
            string oaCron = txtOACron.Text.Trim();
            if (string.IsNullOrEmpty(oaCron))
            {
                MessageBox.Show("间隔时间是必填的！");
                return false;
            }
            return true;
        }


        /// <summary>
        /// OA同步记录数据存储设置
        /// 如果数据大于100条，则清空集合,默认是100条
        /// </summary>
        public void OAListBoxSetting(string msg = "")
        {
            //int oaListBoxNums = Convert.ToInt32(txtListBoxNum.Text.Trim());

            int oaListBoxNums = 100;
            string nums = txtListBoxNum.Text.Trim();
            if (!string.IsNullOrEmpty(nums))
            {
                oaListBoxNums = Convert.ToInt32(nums);
            }

            if (listBoxOAItem.Items.Count >= oaListBoxNums)
            {
                listBoxOAItem.Items.Clear();
            }
            if (msg != "")
            {
                listBoxOAItem.Items.Add(msg);
            }
        }


        /// <summary>
        /// 往OA同步消息里面插入数据
        /// </summary>
        /// <param name="msg"></param>
        public void ShowLog(string msg)
        {

            if (InvokeRequired)
            {
                object[] pList = { msg };
                listBoxOAItem.Invoke(new OAAddToListBox(OAListBoxSetting), pList);
            }
            else
            {
                OAListBoxSetting(msg);
            }

        }

        /// <summary>
        /// 清空同步历史消息数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_oa_listbox_clear_Click(object sender, EventArgs e)
        {
            listBoxOAItem.Items.Clear();
        }

        #endregion


        #region 设备信息定时任务
        public void EquipBaseSetting()
        {
            txtEquipCron.Enabled = false;
            txtEquipListBoxNum.Enabled = false;
            btn_equip_center.Enabled = false;
            btn_equip_edit.Enabled = true;

        }

        public void EquipBaseSettingEdit()
        {
            txtEquipCron.Enabled = true;
            txtEquipListBoxNum.Enabled = true;
            btn_equip_edit.Enabled = false;
            btn_equip_center.Enabled = true;
           
        }

        /// <summary>
        /// 设备定时任务基础设置编辑按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_equip_edit_Click(object sender, EventArgs e)
        {
            EquipBaseSettingEdit();
        }

        /// <summary>
        /// 设备定时任务基础设置确认按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_equip_center_Click(object sender, EventArgs e)
        {
            // 验证用户设置的是否合理
            bool isOk = EquipCenterVaild();

            if (isOk)
            {
                // 初始化设置
                EquipBaseSetting();
            }
        }


        public bool EquipCenterVaild()
        {
            string equipCron = txtEquipCron.Text.Trim();
            if (string.IsNullOrEmpty(equipCron))
            {
                MessageBox.Show("间隔时间是必填的！");
                return false;
            }
            return true;
        }

        public void EquipListBoxSetting(string msg = "")
        {
            int equipListBoxNums = 100;
            string nums = txtEquipListBoxNum.Text.Trim();
            if(!string.IsNullOrEmpty(nums))
            {
                equipListBoxNums = Convert.ToInt32(nums);
            }

            if (listBoxEquip.Items.Count >= equipListBoxNums)
            {
                listBoxEquip.Items.Clear();
            }
            if (msg != "")
            {
                listBoxEquip.Items.Add(msg);
            }
        }

        public void ShowLogEquip(string msg)
        {

            if (InvokeRequired)
            {
                object[] pList = { msg };
                listBoxEquip.Invoke(new OAAddToListBox(EquipListBoxSetting), pList);
            }
            else
            {
                EquipListBoxSetting(msg);
            }

        }

        private void btn_equip_start_Click(object sender, EventArgs e)
        {
            // 禁用启动按钮，开启结束按钮
            btn_equip_end.Enabled = true;
            btn_equip_start.Enabled = false;

            try
            {
                // 获取设置的时间
                //string CronTime = "0/1 * * * * ?";
                string CronTime = txtEquipCron.Text.Trim();
                EquipListBoxSetting();
                listBoxEquip.Items.Add("【" + DateTime.Now.ToString() + "】" + " --*-- 设备数据定时任务开启  --*-- ");
                QuartzUtil.AddJob<EquipJob>("EquipJob", CronTime);

            }
            catch (Exception ex)
            {
                listBoxEquip.Items.Add(ex.Message);
            }
        }

        private void btn_equip_end_Click(object sender, EventArgs e)
        {
            // 禁用结束按钮，开启启动按钮
            btn_equip_start.Enabled = true;
            btn_equip_end.Enabled = false;

            try
            {
                // 结束定时任务
                QuartzUtil.DeleteJob("EquipJob");
                EquipListBoxSetting();
                listBoxEquip.Items.Add("【" + DateTime.Now.ToString() + "】" + " --*-- 设备数据定时任务结束  --*-- ");
            }
            catch (Exception ex)
            {
                listBoxEquip.Items.Add(ex.Message);
            }
        }

        private void btn_equip_listbox_clear_Click(object sender, EventArgs e)
        {
            listBoxEquip.Items.Clear();
        }
        #endregion


    }
}
